#
# Generic Tool chain support.
#
IF(__CHC12_GENERIC_COMPILER)
  RETURN()
ENDIF(__CHC12_GENERIC_COMPILER)
SET(__CHC12_GENERIC_COMPILER 1)


MESSAGE ("Loaded: Generic-chc12.cmake")

SET(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 0)

GET_FILENAME_COMPONENT(CHC12_PATH ${CMAKE_C_COMPILER} PATH)

IF(NOT DEFINED CHC12_PIPER)
    SET(CHC12_PIPER ${CHC12_PATH}/piper)
ENDIF()

IF(NOT DEFINED CHC12_BURNER)
    SET(CHC12_BURNER ${CHC12_PATH}/burner.exe)
ENDIF()

SET(CMAKE_OBJCOPY      FALSE)
SET(CMAKE_OBJDUMP      FALSE)


#
# We have to setup the object type very early, as this can't be overridden later on.
#
SET (CMAKE_C_OUTPUT_EXTENSION        ".o"         CACHE STRING "C compiler object extension."    FORCE)
SET (CMAKE_CXX_OUTPUT_EXTENSION      ".o"         CACHE STRING "C++ compiler object extension."  FORCE)
SET (CMAKE_CXGATE_OUTPUT_EXTENSION   ".o"         CACHE STRING "C++ compiler object extension."  FORCE)

#
# Convince CMake we know our compiler works OK.
# If we don't do this it will do a TRY-COMPILE which will fail to link.
#
SET (CMAKE_C_COMPILER_ID_RUN    TRUE)
SET (CMAKE_C_COMPILER_WORKS     TRUE)
SET (CMAKE_CXX_COMPILER_ID_RUN  TRUE)
SET (CMAKE_CXX_COMPILER_WORKS   TRUE)
SET (CMAKE_CXGATE_COMPILER_ID_RUN  TRUE)
SET (CMAKE_CXGATE_COMPILER_WORKS   TRUE)

SET (CMAKE_C_COMPILER_ID        chc12)
SET (CMAKE_CXX_COMPILER_ID      chc12)
SET (CMAKE_CXGATE_COMPILER_ID   chc12)

#
# Don't generate preprocessed or assembler makefile rules for C/C++ source files.
#
SET (CMAKE_SKIP_PREPROCESSED_SOURCE_RULES    TRUE)
SET (CMAKE_SKIP_ASSEMBLY_SOURCE_RULES        TRUE)

#
# Workaround to inhibit CMake from performing a TRY-COMPILE to determine the size of "void *",
# this size isn't ever used so its actual value doesn't matter.
#
SET (CMAKE_SIZEOF_VOID_P 2)

#
# Library and linker extensions. (Object extensions have to be specified earlier in Toolchain-Freescale-HC12.cmake)
#
SET (CMAKE_FIND_LIBRARY_PREFIXES                     "")
SET (CMAKE_FIND_LIBRARY_SUFFIXES                     ".lib")
SET (CMAKE_STATIC_LIBRARY_PREFIX                     "")
SET (CMAKE_STATIC_LIBRARY_SUFFIX                     ".lib")
SET (CMAKE_LINK_LIBRARY_SUFFIX                        ".lib")
SET (CMAKE_EXECUTABLE_SUFFIX                         ".elf")

#
# Default options applied to all tool invocations
#
SET (CHC12_STANDARD_FLAGS "-ViewHidden -NoEnv -NoBeep -WStdoutOn -WmsgNu=abcde")

SET (CMAKE_DEPFILE_FLAGS_C                       "")
SET (CMAKE_DEPFILE_FLAGS_CXX                     "")
SET (CMAKE_DEPFILE_FLAGS_CXGATE                  "")
SET (CMAKE_DEPFILE_FLAGS_ASM                     "")


#
# C compiler.
#
SET (CMAKE_C_FLAGS_INIT                             "${CMAKE_C_FLAGS_INIT} -Cc -Dinline")
SET (CMAKE_C_FLAGS_DEBUG_INIT                       "${CMAKE_C_FLAGS_DEBUG_INIT}")
SET (CMAKE_C_FLAGS_MINSIZEREL_INIT                  "${CMAKE_C_FLAGS_MINSIZEREL_INIT}")
SET (CMAKE_C_FLAGS_RELEASE_INIT                     "${CMAKE_C_FLAGS_RELEASE_INIT}")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT              "${CMAKE_C_FLAGS_RELWITHDEBINFO_INIT}")
SET (CMAKE_C_STANDARD_LIBRARIES_INIT                "${CMAKE_C_STANDARD_LIBRARIES_INIT}")

#
# C++ compiler
#
SET (CMAKE_CXX_FLAGS_INIT                           "${CMAKE_CXX_FLAGS_INIT} -Cc")
SET (CMAKE_CXX_FLAGS_DEBUG_INIT                     "${CMAKE_CXX_FLAGS_DEBUG_INIT}")
SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT                "${CMAKE_CXX_FLAGS_MINSIZEREL_INIT}")
SET (CMAKE_CXX_FLAGS_RELEASE_INIT                   "${CMAKE_CXX_FLAGS_RELEASE_INIT}")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT            "${CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT}")
SET (CMAKE_CXX_STANDARD_LIBRARIES_INIT              "${CMAKE_CXX_STANDARD_LIBRARIES_INIT}")

#
# CXGATE compiler
#
SET (CMAKE_CXGATE_FLAGS_INIT                           "${CMAKE_CXGATE_FLAGS_INIT} -Cc")
SET (CMAKE_CXGATE_FLAGS_DEBUG_INIT                     "${CMAKE_CXGATE_FLAGS_DEBUG_INIT}")
SET (CMAKE_CXGATE_FLAGS_MINSIZEREL_INIT                "${CMAKE_CXGATE_FLAGS_MINSIZEREL_INIT}")
SET (CMAKE_CXGATE_FLAGS_RELEASE_INIT                   "${CMAKE_CXGATE_FLAGS_RELEASE_INIT}")
SET (CMAKE_CXGATE_FLAGS_RELWITHDEBINFO_INIT            "${CMAKE_CXGATE_FLAGS_RELWITHDEBINFO_INIT}")
SET (CMAKE_CXGATE_STANDARD_LIBRARIES_INIT              "${CMAKE_CXGATE_STANDARD_LIBRARIES_INIT}")

#
# Flags used by the linker.
#
SET (CMAKE_EXE_LINKER_FLAGS_INIT                    "${CMAKE_EXE_LINKER_FLAGS_INIT} -B -EnvSRECORD=S19")
SET (CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT              "${CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT}")
SET (CMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT         "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT}")
SET (CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT            "${CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT}")
SET (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT     "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")

SET (CMAKE_MODULE_LINKER_FLAGS_INIT                 "${CMAKE_MODULE_LINKER_FLAGS_INIT}")
SET (CMAKE_MODULE_LINKER_FLAGS_DEBUG_INIT           "${CMAKE_MODULE_LINKER_FLAGS_DEBUG_INIT}")
SET (CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL_INIT      "${CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL_INIT}")
SET (CMAKE_MODULE_LINKER_FLAGS_RELEASE_INIT         "${CMAKE_MODULE_LINKER_FLAGS_RELEASE_INIT}")
SET (CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO_INIT  "${CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO_INIT}")

SET (CMAKE_SHARED_LINKER_FLAGS_INIT                 "${CMAKE_SHARED_LINKER_FLAGS_INIT}")
SET (CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT           "${CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT}")
SET (CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL_INIT      "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL_INIT}")
SET (CMAKE_SHARED_LINKER_FLAGS_RELEASE_INIT         "${CMAKE_SHARED_LINKER_FLAGS_RELEASE_INIT}")
SET (CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO_INIT  "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO_INIT}")

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_INIT}")
SET(CMAKE_CXGATE_FLAGS "${CMAKE_CXGATE_FLAGS_INIT}")

#
# Compiler invocation.
#
SET (CMAKE_INCLUDE_SYSTEM_FLAG_ASM       "-EnvLIBPATH=")
SET (CMAKE_INCLUDE_SYSTEM_FLAG_C         "-EnvLIBPATH=")
SET (CMAKE_INCLUDE_SYSTEM_FLAG_CXX       "-EnvLIBPATH=")
SET (CMAKE_INCLUDE_SYSTEM_FLAG_CXGATE    "-EnvLIBPATH=")

SET (CMAKE_C_COMPILE_OBJECT              "<CMAKE_C_COMPILER>   ${CHC12_STANDARD_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -ObjN=<OBJECT> -Lasm=<OBJECT>.lst <SOURCE>")
SET (CMAKE_ASM_COMPILE_OBJECT            "<CMAKE_ASM_COMPILER> ${CHC12_STANDARD_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -ObjN<OBJECT> <SOURCE>")
SET (CMAKE_CXX_COMPILE_OBJECT            "<CMAKE_CXX_COMPILER> ${CHC12_STANDARD_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -ObjN=<OBJECT> -Lasm=<OBJECT>.lst <SOURCE>")
SET (CMAKE_CXGATE_COMPILE_OBJECT         "<CMAKE_CXGATE_COMPILER> ${CHC12_STANDARD_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -ObjN=<OBJECT> -Lasm=<OBJECT>.lst <SOURCE>")

#
# Librarian invocation.
#
SET (CMAKE_C_CREATE_STATIC_LIBRARY       "<CMAKE_AR> ${CHC12_STANDARD_FLAGS} -Mar{<TARGET> <OBJECTS>}")
SET (CMAKE_CXX_CREATE_STATIC_LIBRARY     "${CMAKE_C_CREATE_STATIC_LIBRARY}")
SET (CMAKE_CXGATE_CREATE_STATIC_LIBRARY  "${CMAKE_C_CREATE_STATIC_LIBRARY}")

#
# Link support.
#
SET (CMAKE_LIBRARY_PATH_FLAG "-L")
SET (CMAKE_LINK_LIBRARY_FLAG "")

#
# Link invocation.
#

MESSAGE("LINK_LIBRARIES: ${LINK_LIBRARIES}")
SET (CMAKE_C_LINK_EXECUTABLE             "<CMAKE_LINKER> ${CHC12_STANDARD_FLAGS} <CMAKE_C_LINK_FLAGS> <CMAKE_CXGATE_LINK_FLAGS> <LINK_FLAGS> -Add{<LINK_LIBRARIES>} -Add{<OBJECTS>} -Add{${HC12_RTL_LIBS}} -O<TARGET>")
SET (CMAKE_CXX_LINK_EXECUTABLE           "${CMAKE_C_LINK_EXECUTABLE}")
SET (CMAKE_CXGATE_LINK_EXECUTABLE        "${CMAKE_C_LINK_EXECUTABLE}")
